/*
 * TelaProdutoCadastro.java
 *
 * Created on __DATE__, __TIME__
 */

package telas.aluguel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import business.aluguel.Produto;
import business.sistema.ExceptionSistema;
import business.sistema.IObjetoSistema;
import business.util.Menssagens;
import telas.comum.TelaDialogoCadastro;

/**
 *
 * @author  __USER__
 */
public class TelaProdutoCadastro extends TelaDialogoCadastro
{
	private static final long serialVersionUID = 726943197470254800L;

	/** Creates new form TelaProdutoCadastro */
	public TelaProdutoCadastro(java.awt.Frame parent, boolean modal)
			throws Exception
	{
		super(parent, modal);
		initComponents();
	}

	/** Creates new form TelaProdutoCadastro */
	public TelaProdutoCadastro(IObjetoSistema objetoSistema, boolean novo)
			throws Exception
	{
		super(objetoSistema, novo);
		initComponents();

		if (!novo)
		{
			setTitle("Alterar Produto");
			preencherFormulario();
		} else
		{
			setTitle("Cadastrar Produto");
		}

		eventoSalvar();
		eventoFechar();

		setVisible(true);
	}
	
	@Override
	public void preencherFormulario() throws Exception
	{
		Produto produto = (Produto)getObjDados();
		
		ctNome.setText(produto.getNome());
		cndValor.setText(String.valueOf(produto.getValor()));
	}
	
	private void eventoSalvar()
	{
		botBotoes.getBtSalvar().addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					salvar();
					limpaCadastro();
					
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}
	
	@Override
	public void preencheObjeto() throws Exception
	{
		String mensagem = "";
		
		if(ctNome.getText().equals(""))
		{
			mensagem += "Nome " + Menssagens.itemObrigatorio;
		}
		
		if(cndValor.getText().equals(""))
		{
			mensagem += "Valor " + Menssagens.itemObrigatorio;
		}
		
		if(mensagem.equals(""))
		{
			Produto produto = (Produto)getObjDados();
			
			produto.setNome(ctNome.getText());
			produto.setValor(Double.parseDouble(cndValor.getNumero()));
		}
	}
	
	@Override
	public void limpaCadastro() throws Exception
	{
		ctNome.setText("");
		cndValor.setText("0.00");
		
		ctNome.requestFocus();
	}
	
	private void eventoFechar()
	{
		botBotoes.getBtFechar().addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					fecharTela();			
				} 
				catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{

		pnlPrincipal = new telas.componentes.Painel();
		botBotoes = new telas.componentes.BarraOperacoesTela();
		pnlCadastro = new telas.componentes.Painel();
		rotulo1 = new telas.componentes.Rotulo();
		rotulo2 = new telas.componentes.Rotulo();
		ctNome = new telas.componentes.CampoTexto();
		cndValor = new telas.componentes.CampoNumeroDecimal();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		pnlPrincipal.setLayout(new java.awt.BorderLayout());
		pnlPrincipal.add(botBotoes, java.awt.BorderLayout.PAGE_END);

		pnlCadastro.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

		rotulo1.setText("Nome:");

		rotulo2.setText("Valor:");

		ctNome.setMaxLength(100);

		javax.swing.GroupLayout pnlCadastroLayout = new javax.swing.GroupLayout(
				pnlCadastro);
		pnlCadastro.setLayout(pnlCadastroLayout);
		pnlCadastroLayout
				.setHorizontalGroup(pnlCadastroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlCadastroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																rotulo2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																ctNome,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																551,
																Short.MAX_VALUE)
														.addComponent(
																cndValor,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																153,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		pnlCadastroLayout
				.setVerticalGroup(pnlCadastroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlCadastroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																ctNome,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cndValor,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pnlPrincipal.add(pnlCadastro, java.awt.BorderLayout.CENTER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 625,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 114,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private telas.componentes.BarraOperacoesTela botBotoes;
	private telas.componentes.CampoNumeroDecimal cndValor;
	private telas.componentes.CampoTexto ctNome;
	private telas.componentes.Painel pnlCadastro;
	private telas.componentes.Painel pnlPrincipal;
	private telas.componentes.Rotulo rotulo1;
	private telas.componentes.Rotulo rotulo2;
	// End of variables declaration//GEN-END:variables

}